package spark.example

import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.SparkContext._
object wordcountscala {
  def main(args: Array[String]) {
    if (args.length < 2) {
      System.err.println("Usage: <file>")
      System.exit(1)
    }

    // val conf = new SparkConf().setAppName("wordcount").setMaster("spark://master:7077")
    val conf = new SparkConf().setAppName("wordcount").setMaster("local")
    val sc = new SparkContext(conf)
    val line = sc.textFile(args(0))
    //output to file
    //line.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_).saveAsTextFile(args(1))
    //output to  screen
    line.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_).collect().foreach(println)
    sc.stop()
  }
}
